//
//  ST_ChatMessageTailControl.swift
//  btvideo
//
//  Created by mac on 2020/11/11.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyUserDefaults


enum ST_ChatMsgTailEnum:Int{
    case hidden = 0,free,willIncome,replied,expire,other
}

class ST_ChatMessageTailControl: UIControl {
    
    
//    widht 100 , height 17
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(left_image_view)
        self.addSubview(titleLabel)
        self.addSubview(tick_image_view)
        
        
        left_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.width.height.equalTo(14)
            make.centerY.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(0)
            make.centerY.equalToSuperview()
        }
        
        tick_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right).offset(2)
            make.width.height.equalTo(13)
            make.centerY.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var left_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("incomeIconSmall")!)
        control.isHidden = true
          return control
      }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var tick_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("tickIcon")!)
        control.isHidden = true
          return control
      }()
   
    
    var model = ST_ChatMessageTailControlModel(){
        didSet{
            
            if model.status == .hidden {
                self.isHidden = true
                return
            }
            
            self.isHidden = false
            
            if model.status == .free {
                left_image_view.isHidden = true
                titleLabel.snp.updateConstraints { (make) in
                    make.left.equalToSuperview().offset(0)
                }
            }else{
                left_image_view.isHidden = false
                titleLabel.snp.updateConstraints { (make) in
                    make.left.equalToSuperview().offset(14+5)
                }
            }
            
            titleLabel.text = model.titleStr
            
            tick_image_view.isHidden = model.status != .replied
            
        }
    }
}


//MARK: - View Model
class ST_ChatMessageTailControlModel: NSObject {
    
    var income:Float = 0.0
    var date:TimeInterval = 0
    
    var status = ST_ChatMsgTailEnum.hidden
    
    var titleStr = ""
    
    /**
     初始化
     Paramters
     income : 收益
     replied : 已经回复 = true
     freeMsgNum : 剩余免费消息X条
     freeSayHiNum : 剩余免费搭讪X次
     */
    convenience init(income:Float,sendDate:TimeInterval,replied:Bool = false,freeMsgNum:Int? = nil,freeSayHiNum:Int? = nil) {
        
        self.init()
        
        self.income = income
        self.date = sendDate
        
     
        
        if income == -67 || income == 0 {
            /**
             新增需求
             1.女生收到免费消息前端显示调整（今日免费消息改为“剩余免费消息X条”）
             2.女生收到免费搭讪前端显示调整（剩余免费搭讪X次）
             */
            if let number = freeMsgNum{
                
                
                
                create_other_sub_string(number: number, str0: SCComponets.rich_text_storage_component.json["chat_msg_tail"][0].stringValue, str1: "条")
                return
            }
            
            if let number = freeSayHiNum{
                create_other_sub_string(number: number+1, str0: SCComponets.rich_text_storage_component.json["chat_msg_tail"][1].stringValue, str1: "次")
                return
            }
        }
        
        switch income {
//        好友 或者 收费 0 没有尾巴
        case -1023,0:
            status = .hidden
        case -67:
            titleStr = SCComponets.rich_text_storage_component.json["chat_msg_tail"][2].stringValue
            status = .free
        default:
            titleStr = "回复得\(Defaults[.PandaStr])"
            status = .willIncome
            
            if replied {
                
                let str = String(format: "+%.2f\(Defaults[.PandaStr])", income)
                titleStr = str
                status = .replied
            }
//            未回复才计算过期
            else{
                
                var isExpire = false
        //        正式服 大于72 过期
                if PH.apiURLType == .release{
                    isExpire = Date().hoursSince(Date(timeIntervalSince1970: sendDate)).abs > 72
                }
        //        测试服 大于10分钟 过期
                else{
                    isExpire = Date().minutesSince(Date(timeIntervalSince1970: sendDate)).abs > 10
                }
                
                if isExpire{
                    titleStr = "过期已退回"
                    status = .expire
                }
                
            }
            
        }
        
    }
    
    
    fileprivate func create_other_sub_string(number:Int,str0:String,str1:String){
        
        var n = number - 1
        if n < 0 {
            n = 0
        }
        
        status = .other
        titleStr = str0+n.string+str1
    }
    
}
